<template>
  <BasicLayout>
    <template #wrapper>
      <el-card class="box-card">
        <el-form
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="提现方式" prop="extractType">
            <el-select
              v-model="queryParams.extractType"
              placeholder="请选择提现方式"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in extractTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="学校ID" prop="schoolId">
            <el-input
              v-model="queryParams.schoolId"
              placeholder="请输入学校ID"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="申请状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="请选择申请状态"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-permisaction="['edu:extract:add']"
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-permisaction="['edu:extract:edit']"
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-permisaction="['edu:extract:remove']"
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              >删除
            </el-button>
          </el-col>
        </el-row>

        <el-table
          v-loading="loading"
          :data="financeextractList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
            align="center"
          /><el-table-column
            label="开户地址"
            align="center"
            prop="bankAddress"
            :show-overflow-tooltip="true"
          /><el-table-column
            label="银行卡号"
            align="center"
            prop="bankCode"
            :show-overflow-tooltip="true"
          /><el-table-column
            label="创建时间"
            align="center"
            prop="createdAt"
            :show-overflow-tooltip="true"
          /><el-table-column
            label="提现金额"
            align="center"
            prop="extractPrice"
            :show-overflow-tooltip="true"
          /><el-table-column
            label="提现方式"
            align="center"
            prop="extractType"
            :formatter="extractTypeFormat"
            width="100"
          >
            <template slot-scope="scope">
              {{ extractTypeFormat(scope.row) }}
            </template> </el-table-column
          ><el-table-column
            label="驳回时间"
            align="center"
            prop="failAt"
            :show-overflow-tooltip="true"
          /><el-table-column
            label="驳回原因"
            align="center"
            prop="failMsg"
            :show-overflow-tooltip="true"
          /><el-table-column
            label="备注"
            align="center"
            prop="mark"
            :show-overflow-tooltip="true"
          /><el-table-column
            label="申请状态"
            align="center"
            prop="status"
            :formatter="statusFormat"
            width="100"
          >
            <template slot-scope="scope">
              {{ statusFormat(scope.row) }}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                v-permisaction="['edu:extract:edit']"
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改
              </el-button>
              <el-button
                v-permisaction="['edu:extract:remove']"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
             <el-form-item
              label="提现金额"
              prop="extractPrice"
              v-if="form.extractType == 'bank'"
            >
              <el-input-number
                v-model="form.extractPrice"
                :min="1"
                :max="10000"
                label="提现金额"
              ></el-input-number>
            </el-form-item>
            
            <el-form-item label="提现方式" prop="extractType">
              <el-radio-group v-model="form.extractType">
                <el-radio
                  v-for="dict in extractTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                  >{{ dict.dictLabel }}</el-radio
                >
              </el-radio-group>
            </el-form-item>

            <el-form-item
              label="收款方开户行"
              prop="bankCode"
              v-if="form.extractType == 'bank'"
            >
              <el-select
                v-model="form.bankCode"
                placeholder="请选择收款方开户行"
              >
                <el-option
                  v-for="dict in bankCodeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>

            <el-form-item
              label="收款方用户名"
              prop="trueName"
              v-if="form.extractType == 'bank'"
            >
              <el-input v-model="form.bankAddress" placeholder="收款方用户名" />
            </el-form-item>

            <el-form-item
              label="收款方银行卡号"
              prop="bankNo"
              v-if="form.extractType == 'bank'"
            >
              <el-input
                v-model="form.bankAddress"
                placeholder="收款方银行卡号"
              />
            </el-form-item>

           

            <el-form-item label="备注" prop="mark">
              <el-input v-model="form.mark" placeholder="备注" />
            </el-form-item>

            <el-form-item
              label="支付宝账号"
              prop="alipayCode"
              v-if="form.extractType == 'alipay'"
            >
              <el-input v-model="form.alipayCode" placeholder="支付宝账号" />
            </el-form-item>
            <el-form-item
              label="微信号"
              prop="wechatCode"
              v-if="form.extractType == 'wx'"
            >
              <el-input v-model="form.wechatCode" placeholder="微信号" />
            </el-form-item>
          </el-form>
          <div>
            <div>说明：</div>
            <div>
              1.到账实效为1-3日，最快次日到账<br />
              2.每笔按付款金额收取手续费，按金额0.1%收取，最低1元，最高25元。<br />
              3.每天限额2万元<br />
            </div>
            <el-link
              href="https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_1&index=1"
              type="primary"
              >提现手续费说明</el-link
            >
          </div>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-card>
    </template>
  </BasicLayout>
</template>

<script>
import {
  addFinanceExtract,
  delFinanceExtract,
  getFinanceExtract,
  listFinanceExtract,
  updateFinanceExtract,
} from "@/api/edu/finance/extract";

export default {
  name: "Config",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isEdit: false,
      // 类型数据字典
      typeOptions: [],
      financeextractList: [],
      extractTypeOptions: [],
      bankCodeOptions: [],
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        extractId: undefined,
        extractType: undefined,
        schoolId: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        extractId: [{ required: true, message: "不能为空", trigger: "blur" }],
        extractType: [
          {
            required: true,
            message: "bank = 银行卡 alipay = 支付宝wx=微信不能为空",
            trigger: "blur",
          },
        ],
        schoolId: [
          { required: true, message: "学校ID不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "-1 未通过 0 审核中 1 已提现不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("edu_extract_type").then((response) => {
      this.extractTypeOptions = response.data;
    });

    this.getDicts("edu_extract_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("edu_bank_code").then((response) => {
      this.bankCodeOptions = response.data;
    });
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listFinanceExtract(
        this.addDateRange(this.queryParams, this.dateRange)
      ).then((response) => {
        this.financeextractList = response.data.list;
        this.total = response.data.count;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        alipayCode: undefined,
        balance: undefined,
        bankAddress: undefined,
        bankCode: undefined,
        extractId: undefined,
        extractPrice: 1,
        extractType: "bank",
        failAt: undefined,
        failMsg: undefined,
        mark: undefined,
        schoolId: undefined,
        schoolName: undefined,
        status: undefined,
        wechatCode: undefined,
      };
      this.resetForm("form");
    },
    extractTypeFormat(row) {
      return this.selectDictLabel(this.extractTypeOptions, row.extractType);
    },
    statusFormat(row) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学校提现表";
      this.isEdit = false;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.extractId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const extractId = row.extractId || this.ids;
      getFinanceExtract(extractId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学校提现表";
        this.isEdit = true;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.extractId !== undefined) {
            updateFinanceExtract(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addFinanceExtract(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.extractId || this.ids;
      this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delFinanceExtract(Ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
  },
};
</script>